"""
漫百库
https://www.manhuabaiku.com/custom/hot
"""

from lxml import etree, html
import requests
from pymongo import MongoClient

client = MongoClient()
db = client.get_database("漫百库")
collection = db.get_collection("top50")

datas = []
url = "https://www.manhuabaiku.com/custom/hot"
res = requests.get(url)
# print(res.text)
tree = etree.HTML(res.text)
books = tree.xpath("//div[@class='cy_ph_list_mh']/ul")
for book in books:
    category_list = []
    main_image = book.xpath("./li[@class='pic']/a/img/@src")[0]
    title = book.xpath("./li[@class='title']/a/text()")[0]
    author = book.xpath("./li[@class='sx']/span/text()")[0][3:]
    state = book.xpath("./li[@class='sx']/span/span[@class='green']/text()")[0]
    categorys = book.xpath("./li[@class='sx']/span[2]/span[2]/a")
    for category in categorys:
        category_child = category.xpath("./text()")[0]
        category_list.append(category_child)
    latest_chapter = book.xpath("./li[@class='sx']/span[2]/span[3]/a/text()")[0]
    info = book.xpath("./li[@class='dec']/text()")[0][3:]
    datas.append({
        "title": title,
        "main_image": main_image,
        "author": author,
        "state": state,
        "category": category_list,
        "latest_chapter": latest_chapter,
        "info": info
    })
    # print(main_image, title, author, state, category_list, latest_chapter, info)
collection.insert_many(datas)
client.close()
